var millName; //染厂名称
var clientName; //客户名称
var m_SDate;
var m_Edate;
var millData = [];
var openID;
var hgS2;
var Judge_plan;
var code = getQueryString("code");

function getQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]);
    return null;
}


$(function () {
    //首先获取当前的起始值
    m_SDate = getDay(-7);
    m_Edate = getDay(0);

    //获取登录用户的权限和对应显示的数据
    getopenid();


});

function getopenid() {
    console.log(
        'https://www.xdfznh.club/wxpadbe/switch/oao/'
        + code);

    $.ajax({
        url: 'https://www.xdfznh.club/wxpadbe/switch/oao/'
            + code,
        type: 'get',
        dataType: 'json',
        // 成功回调
        success: function (data) {
            console.log('请求成功');
            openID = data.data;
            console.log('openID=' + openID);
            getmill();
        },
        //失败回调
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            //查看错误信息
            console.log('失败回调');
            console.log(XMLHttpRequest.status);
            console.log(XMLHttpRequest.readyState);
            console.log(textStatus);
        }
    })
}


//获取微信号对应的染厂名称
function getmill() {

    console.log('https://www.xdfznh.club/wxpadbe/client/' + openID);

    $.ajax({
        url: 'https://www.xdfznh.club/wxpadbe/client/'
            + openID,

        type: 'get',
        dataType: 'json',
        success: function (data) {
            console.log(data.length);

            if (data.length == 0) {
                // 关闭当前页面
                alert('当前用户没有权限！');
            }

            if (data.length > 0) {
                millName = data[0].clients.split("-")[0]; //获取染厂名称
                clientName = data[0].clients.split("-")[1];
                console.log(millName);
                console.log(clientName);
                for (var n = 0; n < data.length; n++) {
                    millData[n] = data[n].clients;

                    //console.log(data[n].mill);
                }
                ;
                getJurisdiction(); //获取个人查看报表的权限
                getDropDownDate(); // 设置下拉

            }
            ;
        }
    })
};

//获取个人查看报表的权限
function getJurisdiction() {
    console.log('https://www.xdfznh.club/wxpadbe/dynamicDS/query/'
        + 'select customer_inout FROM sys_userpermission where openid='
        + "'" +openID  + "' and client_name="
        + "'" + clientName + "' and mill="
        + "'" + millName + "'");

    console.log(millName);

    $.ajax({
       // url: 'https://www.xdfznh.club/wxpadbe'
       //     + '/perm/selectBoss/'
       //     + openID + '/'
       //     + millName,

        url: 'https://www.xdfznh.club/wxpadbe/dynamicDS/query/'
            + 'select customer_inout FROM sys_userpermission where openid='
            + "'" +openID  + "' and client_name="
            + "'" + clientName + "' and mill="
            + "'" + millName + "'",

        type: 'get',
        dataType: 'json',
        success: function (data) {

            if (data.length > 0) {
                millName = data[0].mill;
                var cp = eval('(' + data[0].customer_inout + ')');    /*客户云白坯成品收发*/

                if (cp==1) {
                    Judge_cp = true;
                    getData();
                    getData1();
                    getsum()
                } else {
                    Judge_cp = false;
                    alert('当前用户没有查看成品白坯收发权限！');
                }

                /*
                                for (var n = 0; n < data.length; n++) {
                                    millData[n] = data[n].mill;
                                    console.log(data[n].mill);
                                };
                 */
                // console.log(millData);
                getDropDownDate(); // 设置下拉

            } else {
                Judge_cp = false;
                Judge_bp = false;
                alert('当前用户没有查看报表权限！');
            }
            ;
        }
    })

};


function getData() {
    $.ajax({
        url: 'https://www.xdfznh.club/wxpadbe/dynamicDS/query'
            + "/call ClientReceive('"
            + m_SDate + "','" + m_Edate
            + "','"
            + millName + "','" + clientName
            + "','成品'"
            + ")",


        type: 'get',
        dataType: 'json',
        success: function (data) {
            $('#waterdetai01').datagrid('loadData', data);

        }
    })
};

function getData1() {
    $.ajax({
        url: 'https://www.xdfznh.club/wxpadbe/dynamicDS/query'
            + "/call ClientReceive('"
            + m_SDate + "','" + m_Edate
            + "','"
            + millName + "','" + clientName
            + "','白坯'"
            + ")",


        type: 'get',
        dataType: 'json',
        success: function (data) {
            $('#waterdetai02').datagrid('loadData', data);

        }
    })
};


function formatter01(value, row, index) {
    var b_ps = row.B_CPFHPS;
    var b_qty = row.B_CPFHQty;
    var b_ms = row.B_CPFHMS;
    return '<span style="color:red;">' + b_ps + '</span>' + '/' +
        '<span style="color:black;">' + b_qty + '</span>' + '/' +
        '<span style="color:deepskyblue;">' + b_ms + '</span>';
}

function formatter(value, row, index) {
    var b_ps = row.B_BPRKPS;
    var b_qty = row.B_BPRKQty;
    var b_ms = row.B_CPFHMS;
    return '<span style="color:red;">' + b_ps + '</span>' + '/' +
        '<span style="color:black;">' + b_qty + '</span>';
    //  '<span style="color:black;">'+b_ms+'</span>';
}


function getsum() {
    /*表格数据求和*/

    $('#waterdetai01').datagrid({
        showFooter: true,
        onLoadSuccess: function (data) {
            data = data.rows;
            var cpps = 0;
            var cpqty = 0;
            var cpms = 0;

            for (let i = 0; i < data.length; i++) {
                cpps += data[i].B_CPFHPS ? parseFloat(data[i].B_CPFHPS) : 0;
                cpqty += data[i].B_CPFHQty ? parseFloat(data[i].B_CPFHQty) : 0;
                cpms += data[i].B_CPFHMS ? parseFloat(data[i].B_CPFHMS) : 0;
            }
            $(this).datagrid('reloadFooter', [{
                B_CPFHDate: '合计',
                B_CPFHPS: cpps,
                B_CPFHQty: cpqty,
                B_CPFHMS: cpms
            }])
        }
    })

    $('#waterdetai02').datagrid({
        showFooter: true,
        onLoadSuccess: function (data) {
            data = data.rows;
            var total = 0;
            var bpps = 0;
            var bpqty = 0;
            for (let i = 0; i < data.length; i++) {
                bpps += data[i].B_BPRKPS ? parseFloat(data[i].B_BPRKPS) : 0;
                bpqty += data[i].B_BPRKQty ? parseFloat(data[i].B_BPRKQty) : 0;
            }
            $(this).datagrid('reloadFooter', [{
                B_CPFHDate: '合计',
                B_BPRKPS: bpps,
                B_BPRKQty:bpqty
            }])
        }
    })

};

/******************************************************************************************/

//计算当前日期的前后几天的日期
function getDay(day) {
    var today = new Date();
    var targetday_milliseconds = today.getTime() + 1000 * 60 * 60 * 24 * day;
    today.setTime(targetday_milliseconds); //注意，这行是关键代码
    var tYear = today.getFullYear();
    var tMonth = today.getMonth();
    var tDate = today.getDate();
    tMonth = doHandleMonth(tMonth + 1);
    tDate = doHandleMonth(tDate);
    return tYear + "-" + tMonth + "-" + tDate;
}

//本月第一天
function getOneMonth() {
    var today = new Date();
    var tYear = today.getFullYear();
    var tMonth = today.getMonth();
    tMonth = doHandleMonth(tMonth + 1);
    return tYear + "-" + tMonth + "-" + "01";
}

function doHandleMonth(month) {
    var m = month;
    if (month.toString().length == 1) {
        m = "0" + month;
    }
    return m;
}

/**************************************************************************************/


//查询事件
function getquery() {
    if (Judge_cp == true) {
        getData();
        getData1();
    }

}

/*查询本日的日期设置*/
function getdaydate() {
    m_SDate = getDay(0);
    m_Edate = getDay(0);
    getquery();
}

/*查询昨日的日期设置*/
function getyesterday() {
    m_SDate = getDay(-1);
    m_Edate = getDay(-1);
    getquery();
}

/*查询一周的日期设置*/
function getWeekdate() {
    m_SDate = getDay(-7);
    m_Edate = getDay(0);
    getquery();
}

/*查询一个月的日期设置*/
function getOneMonthdate() {
    m_SDate = getDay(-30);
    m_Edate = getDay(0);
    getquery();
}

/*查询本月的日期设置*/
function getMonthdate() {
    m_SDate = getOneMonth();
    m_Edate = getDay(0);
    getquery();
}


//设置染厂下拉
function getDropDownDate() {
    //console.log('2=' + millData);
    hgS2 = new selectSwiper({
        el: '.select_box2',
        mustSelect: true,
        activeIndex: 0,
        data: millData,
        init: function (index) {
            if (index !== -1) {
                $('.btn2').html(this.data[index]);
            }
        },
        okFunUndefind: function () {
            alert('必须选择一项');
        },
        okFun: function (index) {
            $('.btn2').html(this.data[index]);
            millName = $('button').text().split("-")[0];
            clientName = $('button').text().split("-")[1];
            getJurisdictionmill();

        },
        closeFun: function () {
            console.log('取消');
        },
    });

    $('.btn2').on('click', function () {
        hgS2.openSelectSwiper();

    });
}

//在更换染厂之后  重新刷新数据  并判断个人权限
function getJurisdictionmill() {
    console.log('https://www.xdfznh.club/wxpadbe'
        + '/perm/selectBoss/'
        + openID + '/'
        + millName);

    $.ajax({
       // url: 'https://www.xdfznh.club/wxpadbe'
       //     + '/perm/selectBoss/'
       //     + openID + '/'
       //     + millName,

        url: 'https://www.xdfznh.club/wxpadbe/dynamicDS/query/'
            + 'select customer_inout FROM sys_userpermission where openid='
            + "'" +openID  + "' and client_name="
            + "'" + clientName + "' and mill="
            + "'" + millName + "'",

        type: 'get',
        dataType: 'json',
        success: function (data) {

            if (data.length > 0) {
                var cp = eval('(' + data[0].customer_inout + ')');

                if (cp==1) {
                    Judge_cp = true;
                    getData();
                    getData1();
                    getsum()
                } else {
                    Judge_cp = false;
                    alert('当前用户没有查看成品权限！');
                }

            } else {
                Judge_cp = false;
                Judge_bp = false;
                alert('当前用户没有查看报表权限！');
            }
            ;
        }
    })

}
